<?php
namespace frontend\controllers;

use yii;
use yii\web\Controller;
use frontend\models\TransactionModel as Trmodel;

class TransController extends Controller{

    #事务
	public function actionTransaction(){
		$transaction = Yii::$app->db->beginTransaction();
		
		try{

			$t_moder=new Trmodel();

			$t_moder->name='zhengGreat';
			$t_moder->age='20';
			// $t_moder->save();

			 if(!$t_moder->save())
	       		 throw new \Exception('操作失败！');

			$transaction->commit();  //提交事务
			
			return '添加数据成功';
		}catch (\Exception $e){

			$transaction->rollBack();
			throw $e;  //直接打印抛出的错误

			// $transaction->rollBack();
			// $error = $e->getMessage();  //获取抛出的错误
			// return $error;
		}
    }
    
}